<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class Exchange extends Base
{
    protected $table = 'exchange';
    protected $primaryKey = 'id';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 创建
     * */
    public function create($data = [])
    {
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $bankId = DB::table($this->table)->insertGetId($data);
        return $bankId;
    }

    /**
     * 查询
     * */
    public function get($data = [], $isSingle = false)
    {
        if ($isSingle) {
            $result = DB::table($this->table)->where($data)->first();
            return self::objToArray($result);
        } else {
            $result = DB::table($this->table)->where($data)->get()->toArray();
            return self::objToArray($result);
        }
    }

    /**
     * 修改
     */
    public function edit($data = [])
    {
        if (empty($data['where']) or empty($data['data'])) {
            return false;
        }

        $data['field']['updated_at'] = time();
        $data['field']['status'] = $data['data']['status'];
        $data['field']['user_id'] = $data['data']['user_id'];
        return DB::table($this->table)->where($data['where'])->update($data['field']);
    }

    /**
     * insert
     * */
    public function insert($data = [])
    {
        $result = DB::table($this->table)->insert($data);
        return $result;
    }
}